package com.suixinsanren.design.proxy.study.gumball;

import java.util.Random;

public class HasQuarterState implements State {

    private GumballMachine gumballMachine;
    private Random random = new Random(System.currentTimeMillis());

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("你不能再插入25美分硬币了");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("25美分已退回");
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("你已经转动曲柄");
        int winner = random.nextInt(10);
        if ((winner == 0) && (gumballMachine.getCount() > 1)) {
            gumballMachine.setState(gumballMachine.getWinnerState());
        } else {
            gumballMachine.setState(gumballMachine.getSoldState());
        }
    }

    @Override
    public void dispense() {
        System.out.println("已经没有糖果了");
    }

    @Override
    public String toString() {
        return "has quarter state";
    }
}
